package com.avic.rong.goodsSort.fragment.bz.secondfragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.R;
import com.avic.rong.fragment.BaseLazyFragment;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.adapter.GoodsBzGNRvAdapter;
import com.avic.rong.goodsSort.adapter.GoodsBzGWRvAdapter;
import com.avic.rong.goodsSort.api.GoodsSortApi;
import com.avic.rong.goodsSort.bean.GoodsSortBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.avic.rong.wedgit.treeRvAdapter.FilterAdapter;
import com.avic.rong.wedgit.treeRvAdapter.FilterDao;
import com.avic.rong.wedgit.treeRvAdapter.ItemStatus;
import com.avic.rong.wedgit.treeRvAdapter.OptionDao;
import com.avic.rong.wedgit.treeRvAdapter.TreeListDao;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * 标准类型分类
 */
public class GoodsBiaoZhunBZFragment extends BaseLazyFragment{

//    @BindView(R.id.goods_BiaoZhun_Bz_GN_SelectorImg)
//    ImageView goodsBiaoZhunBzGNSelectorImg;
//    @BindView(R.id.goods_BiaoZhun_Bz_GN_Rv)
//    MaxRecyclerView goodsBiaoZhunBzGNRv;
//    @BindView(R.id.goods_BiaoZhun_Bz_GW_SelectorImg)
//    ImageView goodsBiaoZhunBzGWSelectorImg;
//    @BindView(R.id.goods_BiaoZhun_Bz_GW_Rv)
//    MaxRecyclerView goodsBiaoZhunBzGWRv;

    private GoodsBzGNRvAdapter gnRvAdapter;
    private GoodsBzGWRvAdapter gwRvAdapter;

    @BindView(R.id.recycler_list)
    RecyclerView recycler_list;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;

    private Context mContext;
    private GoodsSortBean goodsSortBean;
//    private List<GoodsSortBean.InternalListBean> gnList;
////    private List<GoodsSortBean.ExternalListBean> gwList;
    //国内国外

    private FilterAdapter filterAdapter;
    private List<TreeListDao<FilterDao, OptionDao>> treeList = new ArrayList<>();

    public GoodsBiaoZhunBZFragment() {
        // Required empty public constructor
    }
    public static GoodsBiaoZhunBZFragment getInstance(Context context) {
        GoodsBiaoZhunBZFragment sf = new GoodsBiaoZhunBZFragment();
        sf.mContext = context;
        return sf;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_biao_zhun_bz, container, false);
        ButterKnife.bind(this, view);

//        goodsBiaoZhunBzGNRv.setNestedScrollingEnabled(false);
//        goodsBiaoZhunBzGWRv.setNestedScrollingEnabled(false);
//        goodsBiaoZhunBzGNSelectorImg.setOnClickListener(this);
//        goodsBiaoZhunBzGWSelectorImg.setOnClickListener(this);

        return view;
    }

    @Override
    public void loadData() {
        GoodsSortApi goodsSortApi = new GoodsSortApi((GoodsSortActivity)getContext(), delCartListener, 1,"bz");
        goodsSortApi.setCache(true);
        HttpManager.getInstance().doHttpDeal(goodsSortApi);
    }

    private List<TreeListDao<FilterDao, OptionDao>> initData(){
        List<TreeListDao<FilterDao, OptionDao>> dataList = new ArrayList<>();


        TreeListDao<FilterDao, OptionDao> daoInternal = new TreeListDao<FilterDao, OptionDao>();
        FilterDao filterDaoTwoInternal = new FilterDao(0,"国内标准" , "国内标准",null,false);
        List<OptionDao> subsInternal = new ArrayList<>();
        for(GoodsSortBean.SubBean temp : goodsSortBean.getInternalList()){
            OptionDao optionDaoTwo = new OptionDao(temp.getCatId(), temp.getCatNameShow(),temp.getCatNameShow());
            subsInternal.add(optionDaoTwo);
        }
        daoInternal.setGroupDao(filterDaoTwoInternal);
        daoInternal.setSubList(subsInternal);

        dataList.add(daoInternal);

        TreeListDao<FilterDao, OptionDao> daoExternal = new TreeListDao<FilterDao, OptionDao>();
        FilterDao filterDaoTwoExternal = new FilterDao(0,"国外标准" , "国外标准",null,false);
        List<OptionDao> subsExternal = new ArrayList<>();
        for(GoodsSortBean.SubBean temp : goodsSortBean.getExternalList()){
            OptionDao optionDaoTwo = new OptionDao(temp.getCatId(), temp.getCatNameShow(),temp.getCatNameShow());
            subsExternal.add(optionDaoTwo);
        }
        daoExternal.setGroupDao(filterDaoTwoExternal);
        daoExternal.setSubList(subsExternal);
        dataList.add(daoExternal);


//        for(int index = 0; index < 2; index++){
//            TreeListDao<FilterDao, OptionDao> dao = new TreeListDao<FilterDao, OptionDao>();
//            FilterDao filterDaoTwo = new FilterDao(index,"title" + index , "title" + index,null,false);
//            List<OptionDao> subs = new ArrayList<>();
//            for(int j = 0; j<500;j++){
//                OptionDao optionDaoTwo = new OptionDao(j,"one" + j,"one" + j);
//                subs.add(optionDaoTwo);
//            }
//            dao.setGroupDao(filterDaoTwo);
//            dao.setSubList(subs);
//            list.add(dao);
//        }
        return dataList;
    }



    //获取数据监听
    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            if (!ConstantsUtils.STATUS_SUCCESS_200.equals(subjects.getCode())){
                ToastTools.showToast(getContext(),"加载数据错误");
                return;
            }
            goodsSortBean = (GoodsSortBean) subjects.getData();

//            gnList = goodsSortBean.getInternalList();
//            gwList = goodsSortBean.getExternalList();
            setAdapter();
        }


        @Override
        public void onCacheNext(String cache) {
            Gson gson = new Gson();
            Type type = new TypeToken<BaseResultEntity<GoodsSortBean>>(){}.getType();
            BaseResultEntity resultEntity = gson.fromJson(cache, type);
            goodsSortBean = (GoodsSortBean)resultEntity.getData();
            setAdapter();

            loading_layout.setStatus(LoadingLayout.SUCCESS);

        }

        @Override
        public void onError(final Throwable e) {
            Logger.e("cjx", "bz拿到数据" + e.getMessage());
            loading_layout.setStatus(LoadingLayout.SUCCESS);
        }
    };

    private void setAdapter() {
        filterAdapter = new FilterAdapter(getContext(),treeList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                if (filterAdapter.getItemViewType(i) == ItemStatus.VIEW_TYPE_SUB_ITEM){
                    return 1;
                }
                return  2;
            }
        });

        recycler_list.setLayoutManager(gridLayoutManager);
        recycler_list.setAdapter(filterAdapter);
        filterAdapter.setData(initData());

//        gnRvAdapter = new GoodsBzGNRvAdapter(getContext(),gnList);
//        gwRvAdapter = new GoodsBzGWRvAdapter(getContext(),gwList);
//
//        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
//        GridLayoutManager manager1 = new GridLayoutManager(getContext(), 2);
//        goodsBiaoZhunBzGNRv.setLayoutManager(manager);
//        goodsBiaoZhunBzGWRv.setLayoutManager(manager1);
//
//        goodsBiaoZhunBzGNRv.setAdapter(gnRvAdapter);
//        goodsBiaoZhunBzGWRv.setAdapter(gwRvAdapter);
    }





//    @Override
//    public void onClick(View v) {
//        int id = v.getId();
//        switch (id){
//            case R.id.goods_BiaoZhun_Bz_GN_SelectorImg:
//                if (gnRvAdapter.getItemCount() == 4){
//                    gnRvAdapter.addItemNums(gnList.size());
//                    gnRvAdapter.notifyDataSetChanged();
//                }else {
//                    gnRvAdapter.addItemNums(4);
//                    gnRvAdapter.notifyDataSetChanged();
//                }
//                break;
//            case R.id.goods_BiaoZhun_Bz_GW_SelectorImg:
//                if (gwRvAdapter.getItemCount() == 4){
//                    gwRvAdapter.addItemNums(gwList.size());
//                    gwRvAdapter.notifyDataSetChanged();
//                }else {
//                    gwRvAdapter.addItemNums(4);
//                    gwRvAdapter.notifyDataSetChanged();
//                }
//                break;
//        }
//    }


}
